package free.order.security.action;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Required;

import free.order.common.Page;
import free.order.common.action.BaseAction;
import free.order.security.entity.User;
import free.order.security.entity.Role;
import free.order.security.service.ServiceException;
import free.order.security.service.SecurityService;

/**
 * 用户管理Action.
 * 
 * @see CRUDActionSupport
 * 
 * @author calvin
 */
public class UserAction extends BaseAction {

	private static final long serialVersionUID = -2180690009159324387L;

	private SecurityService securityServ;

	//for register page
	private User user;

	private List<Role> allRoles;
	
	private List<Long> checkedRoleIds;
	
	public String doRegister(){
		this.securityServ.saveUser(user);
		return SUCCESS;
	}
	
	//for list user page
	private Page<User> page = new Page<User>();

	public String list() throws Exception {
		page = this.securityServ.getUserDao().getAllByPage(page);
		return "listUser";
	}
	
	
	private Long id;

	private List<User> allUsers;	
	
	public Page<User> getPage() {
		return page;
	}

	public void setPage(Page<User> page) {
		this.page = page;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public List<Role> getAllRoles() {
		return allRoles;
	}
	
	public List<User> getAllUsers() {
		return allUsers;
	}

	public List<Long> getCheckedRoleIds() {
		return checkedRoleIds;
	}

	public void setCheckedRoleIds(List<Long> checkedRoleIds) {
		this.checkedRoleIds = checkedRoleIds;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public String input() throws Exception {
//		allRoles = securityService.getAllRoles();
//		checkedRoleIds = user.getRoleIds();
		return INPUT;
	}

	public String save() throws Exception {
		//根据页面上的checkbox 整合entity的roles Set
		Set<Role> roles = new LinkedHashSet<Role>();
		
		for(Long id:this.checkedRoleIds){
			Role r = new Role();
			r.setId(id);
			roles.add(r);
		}
		user.setRoles(roles);
		securityServ.saveUser(user);
		addActionMessage("保存用户成功");
		return "";
	}
	
	public String delete() throws Exception {
		try {
			securityServ.deleteUser(id);
			addActionMessage("删除用户成功");
		} catch (ServiceException e) {
//			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return "";
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkLoginName() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String loginName = request.getParameter("loginName");
		String orgLoginName = request.getParameter("orgLoginName");

//		if (securityService.isLoginNameUnique(loginName, orgLoginName))
//			return renderText("true");
//		else
			return "false";
	}

	@Required
	public void setSecurityServ(SecurityService securityServ) {
		this.securityServ = securityServ;
	}
}
